﻿using System.Collections;
using System.ComponentModel.Composition;
using Cubicle.Core.Interfaces;
using Cubicle.Core.Processors;

namespace Cubicle.Excel.Processors
{
    [Export("Cubicle.Excel.Processors.MarshalDictionary", typeof(IProcessor<object>))]
    [ExportMetadata("Name", "MarshalDictionary")]
    [ExportMetadata("Description", "Convert IDictionary into Object[,2]")]
    public class MarshalDictionary : ConditionalProcessor<object>
    {
        #region Overrides of Processor<InstructionRequest>

        public override bool Condition(object request)
        {
            return request is IDictionary;
        }

        public override object ConditionalProcess(object request)
        {
            var dict = (IDictionary)request;
            var result = new object[dict.Count,2];
            var i = 0;
            foreach (var key in dict.Keys)
            {
                result[i, 0] = key;
                result[i, 1] = dict[key];
                i++;
            }
            return result;
        }

        #endregion
    }
}